import 'dart:io';
import 'package:flutter_picker/flutter_picker.dart';
import 'package:hrf_app/common/api/bug/bug.dart';
import 'package:hrf_app/common/api/material/material.dart';
import 'package:hrf_app/common/models/bug/file_model.dart';
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/route_manager.dart';
import 'package:get/state_manager.dart';
import 'package:hrf_app/common/models/material/material.dart';
import 'package:hrf_app/common/models/material/material_file_model.dart';
import 'package:image_picker/image_picker.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class MaterialDetailPageController extends GetxController {
  MaterialDetailPageController();

  MaterialModel materialModel = MaterialModel();
  List<UploadFileModel> uploadFileModelList = [];

  _initData() {
    String id = Get.arguments;
    getMaterialDetailById(id);
  }

  getMaterialDetailById(id) async {
    List<UploadFileModel> uploadFileList = [];
    MaterialApi.getMaterialByIdApi({"id": id}).then((res) {
      if (res["code"] == "00000") {
        materialModel = MaterialModel.fromJson(res["data"]);
        List<MaterialFileModel> fileList = materialModel.materialFileVoList!;
        if (fileList.length != 0) {
          for (var vo in fileList) {
            UploadFileModel uploadFileModel = UploadFileModel();
            uploadFileModel.url = "/${vo.url!}";
            uploadFileList.add(uploadFileModel);
          }
        }
        uploadFileModelList = uploadFileList;
        update(["materialDetailPage"]);
      }
    });
  }

  Future<void> selectPhoto(ImageSource source) async {
    List<File> images = [];
    final picker = ImagePicker();
    final XFile? pickedFile = await picker.pickImage(source: source);
    if (pickedFile != null) {
      images.add(File(pickedFile.path));
    }
    uploadImg(images);
  }

  Future<void> selectMultiImage() async {
    List<File> images = [];
    final picker = ImagePicker();
    final List<XFile> pickedFileList = await picker.pickMultiImage();
    List<File> imageList = [];
    imageList = pickedFileList.map((file) => File(file.path)).toList();
    images.addAll(imageList);
    uploadImg(images);
  }

  Future<void> uploadImg(List<File> imgList) async {
    // 单个文件上传
    List<MultipartFile> multipartFile = [];
    for (var element in imgList) {
      MultipartFile mfile = await MultipartFile.fromFile(
        element.path,
      );
      multipartFile.add(mfile);
    }
    FormData formData = FormData.fromMap({'fileList': multipartFile});
    BugApi.simpleUploadListApi(formData).then((res) {
      if (res["code"] == "00000") {
        List<dynamic> imgUrlMap = res['data'];
        for (var itemVo in imgUrlMap) {
          UploadFileModel uploadFileModel = UploadFileModel();
          uploadFileModel.url = "/" + itemVo;
          uploadFileModelList.add(uploadFileModel);
        }
        TDToast.showSuccess("上传成功", context: Get.context!);
        update(["materialDetailPage"]);
      }
    });
  }


    @override
    void onReady() {
      _initData();
      super.onReady();
    }

    void delImage(index) {
      if (index >= 0 && index < uploadFileModelList.length) {
        uploadFileModelList.removeAt(index);
        update(["materialDetailPage"]);
      }
    }

    @override
    void onClose() {
      super.onClose();
    }
  }
